import 'package:fluent_emoji/fluent_emoji.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vischat/modules/home/socialize/chat/message_send_controller.dart';

class MessageInputController extends GetxController {
  final MessageSendController _sendController;
  MessageInputController(this._sendController);

  final _textController = TextEditingController();
  final _focusNode = FocusNode();

  final _allEmojis = FluentEmojis.smileysAndEmotion.allEmojis;
  final _speaking = false.obs;
  final _showVoiceMode = false.obs;
  final _showMorePanel = false.obs;
  final _showEmoji = false.obs;

  get textController => _textController;
  get focusNode => _focusNode;
  get speaking => _speaking.value;
  get showVoiceMode => _showVoiceMode.value;
  get showMorePanel => _showMorePanel.value;
  get showEmoji => _showEmoji.value;
  get allEmojis => _allEmojis;

  void sendTextMessage(String text) async {
    _textController.clear();
    _sendController.sendText(text);
  }

  void selectEmoji(String emoji) {
    _textController.text += emoji;
  }

  void toggleVoiceModel() {
    hideOptionsPanel();
    hideEmoji();
    _showVoiceMode.value = !_showVoiceMode.value;
    if (_showVoiceMode.value) {
      _focusNode.unfocus();
    } else {
      Future.delayed(Duration(milliseconds: 50), () {
        _focusNode.requestFocus();
      });
    }
  }

  void toggleSpeakState(bool val) {
    _speaking.value = val;
  }

  void togglePanel() {
    _focusNode.unfocus();
    hideEmoji();
    _showMorePanel.value = !_showMorePanel.value;
  }

  void toggleEmoji() {
    _focusNode.unfocus();
    hideOptionsPanel();
    _showEmoji.value = !_showEmoji.value;
  }

  void hideOptionsPanel() {
    _showMorePanel.value = false;
  }

  void hideEmoji() {
    _showEmoji.value = false;
  }

  @override
  void onClose() {
    _textController.dispose();
    _focusNode.dispose();
    super.onClose();
  }
}
